<template>
  <base-breadcrumb :title="titleName">
    <searchData
      :search-data="searchArr"
      @search="getTableData"
    />
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button
          v-btn-permission="'lcm_003_add_02'"
          type="primary"
          @click="getAddCase(false)"
        >
          新增执行案件
        </a-button>
        <excel
          ref="exportBtn"
          v-btn-permission="'lcm_003_exp_05'"
          file-name="西南公司执行案件台账"
          content-title="西南公司执行案件台账"
          is-click-generate="false"
        >
          <a-button
            slot="excelBtn"
            type="primary"
            :loading="loadingState.isExport"
            @click="handleExport"
          >
            导出
          </a-button>
        </excel>
      </div>
      <list-table
        ref="table"
        size="default"
        row-key="id"
        :columns="columns"
        :request="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
        :pagination="pagination"
      >
        <template
          slot="caseStatus"
          slot-scope="text"
        >
          {{ resolveStatus(text) }}
        </template>
        <template
          slot="action"
          slot-scope="text, record"
        >
          <MoreButton>
            <a
              v-btnPermission="'lcm_003_loo_01'"
              @click="handleUpdate('look', record)"
            >查看</a>
            <a
              v-if="(record.caseStatus === 'NO_OUTPUT' && record.creatorId === $userInfo().id) && hideBuquBtn()"
              @click="getAddCase(record.id)"
            >编辑</a>
            <a
              v-if="record.caseStatus === 'NO_OUTPUT'"
              v-btnPermission="'lcm_003_jz_03'"
              @click="handleUpdate('look', record, 'progress')"
            >进展情况</a>
            <a
              v-if="record.caseStatus === 'NO_OUTPUT'"
              v-btnPermission="'lcm_003_pz_04'"
              @click="handleUpdate('look', record, 'comment')"
            >批注</a>
            <a
              v-if="(record.caseStatus === 'NO_OUTPUT' && record.creatorId === $userInfo().id) && hideBuquBtn()"
              @click="getOutPut(record)"
            >销项</a>
          </MoreButton>
        </template>
        <div
          slot="expandedRowRender"
          slot-scope="record"
          style="margin: 0"
        >
          <p>
            备注：<span>{{ record.remark }}</span>
          </p>
          <p>
            主要内容：<span>{{ record.content }}</span>
          </p>
          <p>
            发函单位：<span>{{ record.seddingUnit }}</span>
          </p>
        </div>
      </list-table>
    </a-card>
    <out-put
      :visible="visible"
      :loading.sync="loading"
      :title="title"
      :form="chooseRow"
      @ok="getPost"
      @cancel="visible = false"
    />
  </base-breadcrumb>
</template>

<script>
import { getList, destroyLetter, exportData } from '@/api/legal/caseAndLetter/caseManage.js'
import { deleteByExecutionCase, getCaseManagement, postOutPut } from '@/api/legal'
import STable from '@/components/Table'
import OutPut from '../outPut'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { CASE_STATUS_MAP } from '@/config/businessConfig.js'

export default {
  name:'a'+Date.now(),
  components: {
    STable,
    OutPut
  },
  data() {
    return {
      loadingState: {
        isExport: false
      },
      queryParam: {},
      businessCode: 'LCM01',
      fileArrays: [],
      titleName: undefined,
      rogerThatTime: undefined, //收到时间
      theTermTime: undefined, //期限
      advanced: false,
      branchCompanyList: [], //所属分公司列表
      projectList: [], //项目列表
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getList(requestParameters).then((res) => {
          return res
        })
      },
      searchArr: [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   type: SelectSubCompany,
        //   valueKey: 'branchComId'
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId',
        //   keyValue: 'branchComId',
        // },
        {
          name: '状态',
          value: 'caseStatus',
          type: 'select',
          data: Object.keys(CASE_STATUS_MAP).map((key) => {
            return {
              label: CASE_STATUS_MAP[key],
              value: key
            }
          })
        },
        {
          name: '函件名称',
          value: 'name',
          type: 'input'
        },
        {
          name: '收到时间',
          value: 'getDate',
          type: 'dateRange',
          attr: {
            rangeFormat: ['getStartDate', 'getEndDate']
          }
        },
        {
          name: '函件编号',
          value: 'code',
          type: 'input'
        },
        {
          name: '被执行人',
          value: 'byExecutor',
          type: 'input'
        },
        {
          name: '申请执行人',
          value: 'executor',
          type: 'input'
        }
      ],
      columns: [
        {
          title: '所属分公司',
          dataIndex: 'projectInfo.branchComName'
        },
        {
          title: '项目名称',
          dataIndex: 'projectInfo.projectName'
        },
        {
          title: '函件编号',
          dataIndex: 'code'
        },
        {
          title: '函件名称',
          dataIndex: 'name'
        },
        {
          title: '申请执行人',
          dataIndex: 'executor'
        },
        {
          title: '被执行人',
          dataIndex: 'byExecutor'
        },
        {
          title: '收到时间',
          dataIndex: 'receivedDate'
        },
        {
          title: '状态',
          dataIndex: 'caseStatus',
          scopedSlots: { customRender: 'caseStatus' }
        },
        {
          title: '操作',
          width: '230px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      visible: false,
      loading: false,
      outPutId: undefined,
      title: '执行案件销项',
      pagination: true,

      chooseRow: null
    }
  },
  methods: {
    handleUpdate(pageType, data = {}, updateType = '') {
      const commonHandler = () => {
        this.$router.push({
          path: '/legal/caselawyerLetter/case/management/update',
          query: {
            id: data.id || '',
            pageType,
            updateType: updateType
          }
        })
      }
      const map = {
        look: commonHandler
      }
      map[pageType] && map[pageType]()
    },
    /**
     * 解析状态值
     */
    resolveStatus(status) {
      return CASE_STATUS_MAP[status] || ''
    },
    /**
     * 导出搜索条件下所有数据
     */
    async handleExport() {
      try {
        if (this.loadingState.isExport) return
        this.loadingState.isExport = true
        const res = await exportData({ needCount: false, ...this.queryParam })
        const list = (res.data || []).map((item, index) => {
          item.index = index + 1
          item.caseStatus = this.resolveStatus(item.caseStatus)
          return item
        })
        const columns = [
          {
            title: '序号',
            dataIndex: 'index'
          },
          {
            title: '所属分公司',
            dataIndex: 'projectInfo.branchComName'
          },
          {
            title: '所属项目',
            dataIndex: 'projectInfo.projectName'
          },
          {
            title: '函件名称',
            dataIndex: 'name'
          },
          {
            title: '函件编号',
            dataIndex: 'code'
          },

          {
            title: '发函单位',
            dataIndex: 'seddingUnit'
          },
          {
            title: '申请执行人',
            dataIndex: 'executor'
          },
          {
            title: '被执行人',
            dataIndex: 'byExecutor'
          },
          {
            title: '收到时间',
            dataIndex: 'receivedDate'
          },
          {
            title: '处理状态',
            dataIndex: 'caseStatus'
          },
          {
            title: '创建人',
            dataIndex: 'creatorName'
          },
          {
            title: '创建时间',
            dataIndex: 'createTime'
          },
          {
            title: '销项时间',
            dataIndex: 'destroyed.createTime'
          },
          {
            title: '销项说明',
            dataIndex: 'destroyed.comment'
          }
        ]
        this.$refs.exportBtn.generateExcel(list, { columns })
      } catch (e) {
        console.error(e)
      }
      this.loadingState.isExport = false
    },
    handleDel(id) {
      let params = id ? [id] : this.selectId
      this.$delList2(
        deleteByExecutionCase,
        () => {
          console.log(1111)
          this.$refs.table.refresh(true)
        },
        { ids: params }
      )
    },
    fileArrysData(val) {
      this.fileArrays = val
      console.log('返回上传数组数据666:', val)
    },
    handelCancel() {
      this.visible = false
    },
    /**
     * 搜索
     */
    getTableData(val) {
      val && (this.queryParam = val)
      this.$refs.table.refresh()
      // this.queryParam = {}
      // this.rogerThatTime = undefined
      // this.theTermTime = undefined
    },
    /**
     * 新增or编辑
     * @param id
     */
    getAddCase(id) {
      if (id) {
        this.$router.push({
          path: '/legal/caselawyerLetter/case/management/edit',
          query: {
            id: id
          }
        })
      } else {
        this.$router.push('/legal/caselawyerLetter/case/management/add')
      }
    },
    /**
     * 查看
     * @param id
     */
    getDetails(id) {
      this.$router.push({
        path: '/legal/caselawyerLetter/case/management/details',
        query: {
          id: id
        }
      })
    },
    /**
     * 销项
     * @param 当前行
     */
    getOutPut(record) {
      this.visible = true
      this.outPutId = record.id
      this.businessId = record.id
      this.chooseRow = record
      this.loading = false
    },
    getPost(subQueryParam) {
      subQueryParam.executionId = this.outPutId
      subQueryParam.fileInfoList =
        (subQueryParam.fileInfoList && subQueryParam.fileInfoList.map((item) => item.commonId)) || []
      this.loading = true
      destroyLetter(subQueryParam)
        .then((res) => {
          this.loading = false
          if (res.code === 200 || res.data.code === 200) {
            this.$refs.table.refresh()
            this.$message.success('提交成功')
            this.visible = false
          } else {
            this.$message.warning('提交失败')
          }
        })
        .catch((err) => {
          this.loading = false
        })
    },
    getSeeyou(id) {
      this.$router.push({
        path: '/legal/caselawyerLetter/case/management/seeyou',
        query: {
          id: id
        }
      })
    }
  }
}
</script>


<style lang="less" scoped>
/deep/ .table-page-search-wrapper .ant-form-inline .ant-form-item > .ant-form-item-label {
  min-width: 90px;
}

/deep/ .ant-table-thead > tr > th,
.ant-table-tbody > tr > td {
  padding: 10px;
}
</style>
